package com.wikilife;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.oklib.base.CoreBaseNormalFragment;
import com.oklib.widget.recyclerview.BaseQuickAdapter;
import com.oklib.widget.recyclerview.BaseViewHolder;
import com.oklib.widget.recyclerview.CoreRecyclerView;
import com.oklib.widget.recyclerview.listener.OnItemClickListener;
import com.wikilife.index.model.IndexEntrance;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Damon.Han on 2017/2/17 0017.
 *
 * @author Damon
 */

public class FragmentTop extends CoreBaseNormalFragment {
    @BindView(R.id.recycler)
    CoreRecyclerView coreRecyclerView;
    List<IndexEntrance> indexTopItems = new ArrayList<>();

    public static FragmentTop newInstance(int from, int to) {
        FragmentTop fragmentTop = new FragmentTop();
        Bundle bundle = new Bundle();
        bundle.putSerializable("from", from);
        bundle.putSerializable("to", to);
        fragmentTop.setArguments(bundle);
        return fragmentTop;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_index_top;
    }

    private void initData() {
        int from = getArguments().getInt("from", 0);
        int to = getArguments().getInt("to", 0);
        String[] txts = getResources().getStringArray(R.array.index_top_items);
        IndexEntrance indexTopItem = null;
        for (int i = from; i <= to; i++) {
            indexTopItem = new IndexEntrance();
            indexTopItem.text = txts[i];
            int drawableId = getResources().getIdentifier("ic_category_" + i, "mipmap", getContext().getPackageName());
            indexTopItem.drawId = drawableId;
            indexTopItems.add(indexTopItem);
        }
    }

    @Override
    protected void onInit() {
        coreRecyclerView.init(new GridLayoutManager(getContext(), 5), new BaseQuickAdapter<IndexEntrance, BaseViewHolder>(R.layout.layout_img_with_txt) {
            @Override
            protected void convert(BaseViewHolder helper, IndexEntrance item) {
                helper.setText(R.id.txt, item.text);
                helper.setImageResource(R.id.img, item.drawId);
            }
        });
        initData();
        coreRecyclerView.getAdapter().addData(indexTopItems);
        coreRecyclerView.addOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                toast(indexTopItems.get(position).text);
            }
        });
    }

}
